package b1.scenes.fight
{
	import b1.conifg.*;
	import b1.controls.basic.*;
	import b1.net.*;
	import b1.scenes.*;
	import b1.utils.*;
	
	import com.greensock.*;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	import flash.ui.Mouse;

	public class MagicIcon extends Sprite
	{
		private var tip:Sprite;
//		private var iconImage:Sprite;
		private var magicIndex:int;
		private var magicLevel:int;
		private var cooldown:int;
		private var valid:Boolean;
		private const iconWidth:Number = 60;
		
		public function MagicIcon(magicIndex:int, magicLevel:int = 0, cooldown:int = 0, valid:Boolean = true)
		{
			super();
			this.magicIndex = magicIndex;
			this.magicLevel = magicLevel;
			this.cooldown = cooldown;
			this.valid = valid;
			var magicConfig:MagicConfig = Config.magicConfigs[magicIndex];
			//图标
			var IconImage:Class = ResourceHelper.getInstance().getClassByName(magicConfig.iconName);
			var iconImage:Sprite = new IconImage();
			addChild(iconImage);
			if (cooldown > 0)
			{
				//冷却图标
				var CooldownIcon:Class = ResourceHelper.getInstance().getClassByName("CooldownIcon");
				var cooldownIcon:MovieClip = new CooldownIcon();
				var frameIndex:int = Math.ceil(cooldownIcon.totalFrames - cooldown * cooldownIcon.totalFrames /
					Math.max(magicConfig.cooldown[magicLevel], 8));
				cooldownIcon.gotoAndStop(frameIndex);
				addChild(cooldownIcon);
				//圆形背景
				var circleBgr:Sprite = new Sprite();
				circleBgr.graphics.lineStyle(2, 0x000000);
				circleBgr.graphics.beginFill(0xFFFFFF);
				circleBgr.graphics.drawCircle(0, 0, 8);
				circleBgr.graphics.endFill();
				circleBgr.alpha = 0.5;
				circleBgr.x = iconWidth / 2;
				circleBgr.y = iconWidth / 2;
				addChild(circleBgr);
				//冷却文字
				var cooldownText:TextField = new TextField();
				cooldownText.selectable = false;
	        	cooldownText.autoSize = TextFieldAutoSize.LEFT;
				cooldownText.defaultTextFormat = new TextFormat("微软雅黑", 10, 0x000000);
				cooldownText.text = cooldown.toString();
				cooldownText.x = (iconWidth - cooldownText.width) / 2;
				cooldownText.y = (iconWidth - cooldownText.height) / 2;
				addChild(cooldownText);
			}
			if (!valid)
				this.filters = [new ColorMatrixFilter(BFont.blackWhiteFilter)];
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			addEventListener(MouseEvent.CLICK, onMouseClick);
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			if (valid && cooldown <= 0)
			{
				Mouse.cursor = "button";
				this.filters = [new ColorMatrixFilter(BFont.brightFilter)];
			}
			if (null == tip)
			{
				var magicConfig:MagicConfig = Config.magicConfigs[magicIndex];
				tip = new Sprite();
				var roundRec:RoundRec = new RoundRec(130, 120, 0.8, BFont.roundRecColor); 
				tip.addChild(roundRec);
				
				//“点击使用”
				var helpMessage:String;
				var helpText:TextField = new TextField();
				helpText.selectable = false;
	        	helpText.width = roundRec.width;
	        	helpText.autoSize = TextFieldAutoSize.CENTER;
				var helpTexFormat:TextFormat = new TextFormat("微软雅黑", 15, 0x000000, true);
				helpTexFormat.leftMargin = 10;
				helpTexFormat.rightMargin = 10;
				if (!valid)
				{
					helpMessage = "体力不够";
					helpTexFormat.color = 0x800000;
				}
				else if (cooldown > 0)
				{
					helpMessage = "尚未冷却";
					helpTexFormat.color = 0x808000;
				}
				else
				{
					helpMessage = "单击使用";
					helpTexFormat.color = 0x008000;
				}
				helpText.defaultTextFormat = helpTexFormat;
				helpText.text = helpMessage;
				helpText.y = 5;
				tip.addChild(helpText);
				//名字
				var nameText:TextField = new TextField();
				nameText.selectable = false;
	        	nameText.autoSize = TextFieldAutoSize.LEFT;
				var nameTexFormat:TextFormat = new TextFormat("微软雅黑", 15, 0x000000, true);
				nameTexFormat.leftMargin = 10;
				nameTexFormat.rightMargin = 10;
				nameText.defaultTextFormat = nameTexFormat;
				nameText.text = magicConfig.name;
				nameText.y = 25;
				tip.addChild(nameText);
				//攻击，消耗体力，描述
				var description:TextField = new TextField();
				description.selectable = false;
	        	description.autoSize = TextFieldAutoSize.LEFT;
	        	description.width = roundRec.width;
	        	description.wordWrap = true;
	        	description.y = 45;
				var texFormat:TextFormat = new TextFormat("微软雅黑", 13, 0x000000);
				texFormat.leftMargin = 10;
				texFormat.rightMargin = 10;
				description.defaultTextFormat = texFormat;
				description.appendText("等级: " + (magicLevel + 1).toString());
				description.appendText("\n消耗体力: " + magicConfig.vitalityCost[magicLevel]);
				description.appendText("\n冷却回合: " + cooldown);
				tip.addChild(description);
				
				tip.x = this.x + (iconWidth - tip.width) / 2;
				tip.y = this.y + iconWidth;
				this.parent.addChild(tip);
			}
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			if (valid && cooldown <= 0)
			{
				Mouse.cursor = "auto";
				this.filters = null;
			}
			if (null !=  tip && null != tip.parent)
        	{
        		tip.parent.removeChild(tip);
        		tip = null;
        	}
		}
		
		private function onMouseClick(event:MouseEvent):void
		{
			if (SceneContainer.getInstance().mouseDisableFlag && !SceneContainer.getInstance().checkOnlyMousePosition(event.currentTarget))
				return;
			if (valid && cooldown <= 0)
			{
				var fight:Fight = SceneContainer.getInstance().currentScene as Fight;
				fight.leftAnimal.useMagic(magicIndex, magicLevel);
				MagicPane.getInstance().close();
				if (SceneContainer.getInstance().step == 20 || SceneContainer.getInstance().step == 21 ||SceneContainer.getInstance().step == 22)
				{
					SceneContainer.getInstance().clearLastStep();
				}
			}
		}
	}
}